#ifndef MAGTRAYSTATE_H
#define MAGTRAYSTATE_H

#include "configManager/configarray.h"
#include "configManager/configobject.h"
#include "errorHandling/silicolerror.h"

class MagTrayState : public ConfigObject
{
    Q_OBJECT

    Q_PROPERTY(ConfigArray *magTrayState READ magTrayState)

public:
    MagTrayState(QObject *parent = nullptr) : ConfigObject(parent)
    {
        m_magTrayState = new ConfigArray(ConfigElementInfo::Int, this);
        init();
    }

    enum State
    {
        Init,
        NoTray,
        RawTray,
        InUse,
        EmptyTray,
        ProductTray
    };

    void resetMagTrayState(int magTrayCount, int trayState = Init)
    {
        while (m_magTrayState->count() > magTrayCount)
        {
            m_magTrayState->remove(m_magTrayState->count() - 1);
        }
        while (m_magTrayState->count() < magTrayCount)
        {
            m_magTrayState->add(m_magTrayState->count());
        }
        for (int i = 0; i < magTrayCount; i++)
        {
            m_magTrayState->setConfig(i, trayState);
        }
    }

    int getMagTrayState(int trayIndex)
    {
        if (trayIndex >= m_magTrayState->count())
        {
            throw SilicolAbort(
                tr("Tray index exceeded limit! Index: %1, Limit: [0, %2)").arg(trayIndex).arg(m_magTrayState->count()),
                EX_LOCATION);
        }
        return m_magTrayState->getConfig(trayIndex).toInt();
    }

    void setMagTrayState(int trayIndex, State state)
    {
        if (trayIndex < 0 || trayIndex >= m_magTrayState->count())
        {
            throw SilicolAbort(
                tr("Tray index exceeded limit! Index: %1, Limit: [0, %2)").arg(trayIndex).arg(m_magTrayState->count()),
                EX_LOCATION);
        }
        m_magTrayState->setConfig(trayIndex, state);
    }

    ConfigArray *magTrayState() const
    {
        return m_magTrayState;
    }

private:
    ConfigArray *m_magTrayState;
};

#endif    // MAGTRAYSTATE_H
